import RenderCell from 'packages/BaseTableCell/renderCell.js';
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { formatDate } from 'pub-bbx-utils';
import { ServiceRecordTypeLabelEnum } from '@model/enum/LabelEnum'
import i18n from '@src/locales';

// 高级搜索
export const getAdvancedFields = (vm) => {
  let that = vm;
  return [
    {
      fieldName: 'executor',
      displayName: i18n.t('common.base.operator'),
      formType: 'user',
      defineType: '',
      setting: {
        isMulti: false, 
      }
    },
    {
      fieldName: 'servicePrice', // serviceId
      displayName: i18n.t('common.base.name'),
      formType: 'select',
      setting: {
        isMulti: false, 
      }
    },
    {
      fieldName: 'serviceItemType',
      displayName: i18n.t('common.form.preview.serviceItem.colum3'),
      formType: 'select',
      setting: {
        isMulti: false, 
        dataSource: that.serviceItemsTypes.map(item => {
          return {
            label: item,
            value: item
          }
        })
      }
    },
    {
      fieldName: 'customer', // cusId
      displayName: i18n.t('common.base.customer'),
      formType: 'text',
    },
    {
      fieldName: 'item',
      displayName: i18n.t('common.form.preview.serviceItem.colum3'),
      formType: 'select',
      setting: {
        isMulti: false, 
        dataSource: [
          {
            text: i18n.t('servicePrice.record.taskSale'),
            value: "工单销售"
          },
          {
            text: i18n.t('servicePrice.record.taskBack'),
            value: '工单退回'
          },
        ]
      }
    },
    {
      fieldName: 'createTime',
      displayName: i18n.t('common.base.time'),
      formType: 'datetime',
    },
  ].map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: null,
      show: true,
      visable: typeof item.visable == 'undefined' ? true : item.visable,
      isSearch: 1,
      isNull: 1,
      isSystem: 1,
      orderId: 1
    }
  });
}

// 列表 && 选择列
export const getColumnFields = (vm) => {
  const that = vm;

  return [
    {
      fieldName: 'item',
      displayName: i18n.t('common.base.type'),
      renderCell: (h, col, row) => {
        return <div>{ ServiceRecordTypeLabelEnum[row.item] || row.item }</div>
      }
    },
    {
      fieldName: 'name',
      displayName: i18n.t('common.form.preview.serviceItem.serviceItemName'),
      renderCell: (h, col, row) => {
        return <div>{ row.service.name }</div>
      }
      // renderCell: (h, col, row) => {
      //   return <div>
      //     <el-button type="text" onClick={e => that.handleOpenDetailView(row)}>{ row.service.name }</el-button>
      //   </div>
      // }
    },
    {
      fieldName: 'serialNumber',
      displayName: i18n.t('common.part.serviceProviderNo'),
      renderCell: (h, col, row) => {
        return <div>{ row.service.serialNumber }</div>
      }
    },
    {
      fieldName: 'serviceItemType',
      displayName: i18n.t('common.form.preview.serviceItem.colum3'),
      renderCell: (h, col, row) => {
        return <div>{ row.service.type }</div>
      }
    },
    {
      fieldName: 'variation',
      displayName: i18n.t('common.form.preview.serviceItem.colum5'),
      renderCell: (h, col, row) => {
        return <div>{ row.variation }</div>
      }
    },
    {
      fieldName: 'createTime',
      displayName: i18n.t('common.base.time'),
      sortable: 'custom',
      sortName: 'createTime',
      isSystem: 1,
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.createTime, 'YYYY-MM-DD HH:mm')}</div>;
      }
    },
    {
      fieldName: 'executor',
      displayName: i18n.t('common.base.operator'),
      renderCell: (h, col, row) => {
        return RenderCell.renderUser.call(that, h, row.executor.displayName, row.executor.userId, false, row.executor.staffId);
      }
    },
    {
      fieldName: 'taskNo',
      displayName: i18n.t('common.task.taskNo'),
      renderCell: (h, col, row) => {
        return <div>{ row.taskNo }</div>
      }
    },
    {
      fieldName: 'cusName',
      displayName: i18n.t('common.form.type.customerName'),
      renderCell: (h, col, row) => {
        return <div>{ row.cusName }</div>
      }
    },
    {
      fieldName: 'cusSN',
      displayName: i18n.t('common.form.type.customerNo'),
      renderCell: (h, col, row) => {
        return <div>{ row.cusSN }</div>
      }
    },
  ].map(item => {
    // 初始化属性
    let field = {
      ...item,
      formType: 'text',
      setting: item.setting || {},
      isSystem: 1,
      isNull: 1,
      isSearch: 1,
      orderId: 1,
      show: true,
      tableName: 'event',
      visable: typeof item.visable == 'undefined' ? true : item.visable
    };
    return formatColumn(field); 
  })
};